package be.kuleuven.cs.mume;
import java.util.ArrayList;
import java.util.HashSet;

import javax.xml.parsers.*;
import javax.xml.xpath.*;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;



public class Data {
	
	Document doc;
	XPath xpath;
	
	public Data(){
		readData("database.xml");	
	}
	
	public void readData(String file){
		try{
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setNamespaceAware(true); 
		
		
		DocumentBuilder builder = factory.newDocumentBuilder();
		doc = builder.parse("database.xml");		
			
		XPathFactory xfactory = XPathFactory.newInstance();
		xpath = xfactory.newXPath();
		
		}catch(Exception E){;};
	}
	
	public ArrayList<Song> getAllSongsByArtist(String artist, ArrayList<String> als){
		ArrayList<Song> al=new ArrayList<Song>();
		try{
		Object resultid = 
			xpath.evaluate("//song[artist='"+artist+"']/id/text()",doc,XPathConstants.NODESET);
		Object resultsongId = 
			xpath.evaluate("//song[artist='"+artist+"']/songId/text()",doc,XPathConstants.NODESET);
		Object resulttitle = 
			xpath.evaluate("//song[artist='"+artist+"']/title/text()",doc,XPathConstants.NODESET);
		NodeList nodesid = (NodeList) resultid;
		NodeList nodessongId = (NodeList) resultsongId;
		NodeList nodestitle = (NodeList) resulttitle;
		for (int i = 0; i < nodesid.getLength(); i++) {
		    al.add(new Song(nodestitle.item(i).getNodeValue(),nodesid.item(i).getNodeValue(),nodessongId.item(i).getNodeValue())); 
		}
		}catch(Exception E){}
		return removeExisting(al,als);
	}
	
	ArrayList<Song> removeExisting(ArrayList<Song> al, ArrayList<String> als){
		for(int i=0; i< al.size();i++)
		{
			if(als.contains(al.get(i).id)){
				al.remove(i);
				i=i-1;
			}
		}
		return al;
	}
	
	public ArrayList<Song> getAllSongsBySong(String Id, ArrayList<String> ex){
		ArrayList<Song> al=new ArrayList<Song>();		
		try{
			Object songIdObject = 
				xpath.evaluate("//song[id='"+Id+"']/songId/text()",doc,XPathConstants.NODESET);
			NodeList nodesId=(NodeList)songIdObject;
			int size=nodesId.getLength();
			String songId=nodesId.item(0).getNodeValue();
			Object resultid = 
				xpath.evaluate("//song[songId='"+songId+"']/id/text()",doc,XPathConstants.NODESET);
			Object resultsongId = 
				xpath.evaluate("//song[songId='"+songId+"']/songId/text()",doc,XPathConstants.NODESET);
			Object resulttitle = 
				xpath.evaluate("//song[songId='"+songId+"']/title/text()",doc,XPathConstants.NODESET);
			NodeList nodesid = (NodeList) resultid;
			NodeList nodessongId = (NodeList) resultsongId;
			NodeList nodestitle = (NodeList) resulttitle;
			for (int i = 0; i < nodesid.getLength(); i++) {
				Song s=new Song(nodestitle.item(i).getNodeValue(),nodesid.item(i).getNodeValue(),nodessongId.item(i).getNodeValue());
				if(!s.id.equals(Id))
					al.add(s);
			}
		}catch(Exception E){}
		return removeExisting(al,ex);
	}
	
	public ArrayList<String> getAllArtistsBySong(String Id, ArrayList<String> als){
		ArrayList<String> al=new ArrayList<String>();
		try{
		Object resultid = 
			xpath.evaluate("//song[id='"+Id+"']/artist/text()",doc,XPathConstants.NODESET);
		NodeList nodes = (NodeList) resultid;
		for (int i = 0; i < nodes.getLength(); i++) {
		    al.add(nodes.item(i).getNodeValue()); 
		}
		}catch(Exception E){}
		return removeDuplicateAndExistingStrings(al,als);
	}
	
	ArrayList<String> removeDuplicateAndExistingStrings(ArrayList<String> al, ArrayList<String> als){
		  HashSet<String> hashSet = new HashSet<String>(al);
		  for(int i=0;i<als.size();i++){
			  hashSet.remove(als.get(i));
		  }
		  return new ArrayList<String>(hashSet) ;
	}
	
}
